#!/usr/bin/perl  -w 
use strict;
use warnings;

#std includes
use POSIX qw(strftime);
use File::Basename;

#ml pkg includes
use MLDBUtils::DBConvert;
use MLDBUtils::DBConnect;
use MLDBUtils::DBCustID;

my $MLPROD_HOME = $ENV{'MLPROD_HOME'};
my $PGNAME=basename($0);
my $PGDIRNAME=dirname($0);
my $PGERR=$PGNAME."[$$]";
my $PGUSER=$ENV{'USER'};
my $PGPWD =$ENV{'PWD'};
if($PGDIRNAME !~ m/^\//) {
  $PGDIRNAME="$PGPWD/$PGDIRNAME";
}




# lock cust schema name from folder path
my $g_dbschema    = undef;    # fetch from the locked path
my $g_mod_ctx     = undef;
my $g_cust_id     = undef;    # query the db to get based on schema name
my $g_hospital_id = 1;        # for now hard coded;

if($PGDIRNAME =~ m/^.*\/cust\/(\w+)\/scripts\/(\w+)(\/.*)?$/) {
  $g_dbschema = $1;
  $g_mod_ctx  = $2;
} else {
  print STDERR "$PGNAME: Unable to get cust context from : <$PGDIRNAME>\n";
  exit 1;
}


# globals
my %args;

my $g_dbbasename="$ENV{USER}";
my $g_dbname=$g_dbbasename . "_db";
my $g_dbuser=$g_dbschema . "_" . $g_dbbasename . "_u";
my $g_dbpasswd = undef;
my $g_in_tbl_name  = $g_dbschema . ".read_insurance0_new_t";
my $g_out_tbl_name = $g_dbschema . ".temp_batch_insurance0_t";
my $g_dbconv;
my $g_dbconn;
my $g_dbcust;
my $g_err;
my $g_exitcode = 0;
my $g_acct_hdr_file = undef;
my $g_batch_acct_hdr_file = undef;
my $g_acct_data_file = undef;
my $g_batch_acct_data_file = undef;
my $g_batch_recv_date=undef;
my $g_batch_run_date=undef;
my $g_batch_acct_file = undef;
my $g_seqnum = 1;

#prototypes def here


# all subs here
sub init_args(@)
{
  return 0 unless ((@_ > 1) && (@_ % 2 == 0));
  my %args = ( @_ );


  foreach my $opt (keys(%args)) {
    if     ($opt =~ /-b/)     { $g_batch_recv_date    = $args{$opt}; }
#   if     ($opt =~ /-ihdr/)  { $g_acct_hdr_file      = $args{$opt}; }
#   elsif  ($opt =~ /-ohdr/)  { $g_batch_acct_hdr_file  = $args{$opt}; }
#   elsif  ($opt =~ /-idata/) { $g_acct_data_file     = $args{$opt}; }
#   elsif  ($opt =~ /-odata/) { $g_batch_acct_data_file = $args{$opt}; }
    else  { print STDERR "$PGNAME: invalid option $opt\n"; return 0}
  }

if($g_batch_recv_date =~ /^([0][1-9]|[1][0-2])([0][1-9]|[1-2][0-9]|[3][0-1])([0-9][0-9])_([0-1][0-9]|[2][0-4])([0-5][0-9]|60)([0-5][0-9]|60)_(\d\d\d\d\d\d)/)
{
    $g_batch_run_date = substr($g_batch_recv_date,0,13);
    $g_batch_run_date =~ s/(\d\d)(\d\d)(\d\d)_(\d\d)(\d\d)(\d\d)/$1\/$2\/$3 $4:$5:$6/;
  } else {
    print STDERR "$PGNAME: invalid batch date and time : $g_batch_recv_date\n";
    return 0;
  }
  return 1;

}

sub usage()
{
  print STDERR "Usage: $PGNAME [-h|--help]  ARGS\n";
  print STDERR "  ARGS are\n";
  print STDERR "  [-h|--help]                     => help\n";
  print STDERR "  [-b batch_recv_date AS MMDDYY_HHMMSS_XXXXXX WHERE XXXXXX is cust_id padded with leading 0]  => run date, map to file name\n";
# print STDERR "  [-ihdr in_acct_hdr_file]        => specify or read from stdin\n";
# print STDERR "  [-ohdr out_batch_acct_hdr_file]   => specify or read from stdin\n";
# print STDERR "  [-idata in_acct_data_file]      => specify or read from stdin\n";
# print STDERR "  [-odata out_batch_acct_data_file] => specify or to stdout\n";
}


# do nothing
sub handler_default
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  return 1;
}

# arg1 row data
sub handler_our_run_date
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  #strftime("%D",localtime());
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $g_batch_run_date;

  return 1;
}

sub handler_cust_id
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $g_cust_id;

  return 1;
}

sub handler_seqnum
{
  my $batch_trans_col_name = shift(@_);
  my $trans_row_hash_ref = shift(@_);
  my $batch_trans_row_hash_ref = shift(@_);

  $batch_trans_row_hash_ref->{$batch_trans_col_name} = $g_seqnum;

  $g_seqnum++;

  return 1;
}


sub handler_hid
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  if ($acct_row_hash_ref->{$batch_acct_col_name} eq "") {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $g_hospital_id;
  }
  else {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =  $acct_row_hash_ref->{$batch_acct_col_name};
  }
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($batch_acct_row_hash_ref->{$batch_acct_col_name});
  return 1;
}



sub handler_treatment_code
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  if ($acct_row_hash_ref->{$batch_acct_col_name} eq "") {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = 1;
  }
  else {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =  $acct_row_hash_ref->{$batch_acct_col_name};
  }

  return 1;
}



sub handler_output_id
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  if ($acct_row_hash_ref->{$batch_acct_col_name} eq "") {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = 1;
  }
  else {
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =  $acct_row_hash_ref->{$batch_acct_col_name};
  }

  return 1;
}





sub handler_acctnum_seq
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} =  1;

  return 1;
}




sub handler_mname
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  # trunctae middle name to length 1 
  my $nm = substr($acct_row_hash_ref->{"mname"}, 0, 1);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($nm);

  return 1;

}


sub handler_client_npi
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $st =  uc($acct_row_hash_ref->{"state"});
  my %st_amr = ('AL' => 1,'AZ' => 1,'CA' => 1,'CO' => 1,'CT' => 1,'DC' => 1,'FL' => 1,'GA' => 1,'HI' => 1,'IA' => 1,'IL' => 1,'IN' => 1,
                'KS' => 1,'LA' => 1,'MA' => 1,'MI' => 1,'MO' => 1,'MS' => 1,'MT' => 1, 'NC' => 1,'NE' => 1,'NJ' => 1,'NM' => 1,'NV' => 1,
                'NY' => 1,'OH' => 1,'OR' => 1,'PA' => 1,'SD' => 1,'TN' => 1,'TX' => 1,'WA' => 1,'WY' => 1);

  if ($acct_row_hash_ref->{'client_npi1'} eq "") {
     if    ($acct_row_hash_ref->{"cust_id"} == 39) {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 'CA';}
     elsif ($acct_row_hash_ref->{"hid"} ne "")     {$batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{"hid"};}
     else  {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 1;}
  }

  
  elsif ($acct_row_hash_ref->{"cust_id"} == 52 && ($st_amr{$st}) ) { $batch_acct_row_hash_ref->{$batch_acct_col_name} = $st;}

  elsif ($acct_row_hash_ref->{"cust_id"} == 52) {$batch_acct_row_hash_ref->{$batch_acct_col_name} = '1';}

  else
  {
     $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{'client_npi1'};
  }

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($batch_acct_row_hash_ref->{$batch_acct_col_name});
  return 1;
}



sub handler_npi
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{"npi1"} ;

  return 1;
}



sub handler_tax_id
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{"tax_id1"} ;

  return 1;
}



sub handler_legacy
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{"legacy1"} ;

  return 1;
}


sub handler_entity_name
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_name1"},0,50) ;

  return 1;
}


sub handler_entity_pay_to_street
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_pay_to_street1"},0,50) ;

  return 1;
}


sub handler_entity_pay_to_city
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_pay_to_city1"},0,30) ;

  return 1;
}


sub handler_entity_pay_to_state
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_pay_to_state1"},0,2) ;

  return 1;
}


sub handler_entity_pay_to_zip
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_pay_to_zip1"},0,10) ;

  return 1;
}


sub handler_entity_information
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_information1"},0,50);

  return 1;
}



sub handler_entity_phone
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($acct_row_hash_ref->{"entity_phone1"},0,14) ;

  return 1;
}




#-----------------------------------------------------
# Here try to split the first name into two parts.
#
# Algorithm:
#    1. If the middle name exists, no change of fn.
#        2. If the first part of the first name has only
#           one or two char(s), no change of fn.
#        3. Cook middle from from second part of fn
#-----------------------------------------------------


sub handler_firstname
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $fn = $acct_row_hash_ref->{$batch_acct_col_name};
  my $mn = undef;
  if( $fn =~ /\,/)  {
          $fn =~ s/\,/ /;
  }
  if ($fn =~ /\w+\s+\w+/ ) {
          $fn =~ s/(\w+)\s+(\w+)/$1 $2/;  #limit the space in middle
  }
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($fn);
  if( $fn =~ /\w+\s+\w/)  {
          $mn = $fn;
          $fn =~ s/^\s*(\w+)\s.*$/$1/;
          $mn =~ s/^\s*\w+\s+(\w).*$/$1/;       # $mn only one char
  }
  if ($fn =~ /^\w{1,2}\b/) {  # first name like "T Mark" exists
          return 1;
  }
  if ($batch_acct_col_name =~ /g_/){
                if ($mn and $batch_acct_row_hash_ref->{'g_mname'} !~ /\w/) {
                        $batch_acct_row_hash_ref->{'g_mname'} = uc($mn);
                        $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($fn);
                }
  } else {
                if ($mn and $batch_acct_row_hash_ref->{'mname'} !~ /\w/) {
                        $batch_acct_row_hash_ref->{'mname'} = uc($mn);
                        $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($fn);
                }
  }
  return 1;
}




sub handler_lastname
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $ln = $acct_row_hash_ref->{$batch_acct_col_name};
  my($front, $tail);
  if( $ln =~ /\,/ ) {
          $ln =~ s/\,/ /;
  }
  $ln =~ s/\./ /g;
  if ( $ln =~ /\w+\s+\w+/ ) {
          $ln =~ s/(\w+)\s+(\w+)/$1 $2/;  #limit the space in middle
  }
#  if ( $ln =~ /\w+\s+\w+/ ) {
#         $front = $tail = $ln;
#         $front =~ s/^\s*(\w+).*$/$1/;
#         $tail  =~ s/^\s*\w+\s+(\w+).*$/$1/;
#         if( $tail =~ /^(Jr|Sr|II+|I\b|IV\b|VI+|V\b)/i ) {
#               $ln = $front;
#         }
#         elsif( $front =~ /^(Jr|Sr|II+|I\b|IV\b|VI+|V\b)/i ) {
#               $ln = $tail;
#         }
#  }
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($ln);
  return 1;
}



sub handler_relationship
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  # trunctae relationship flag to length 3 
  my $nm = substr($acct_row_hash_ref->{"relationship_flag"}, 0, 3);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($nm);

  return 1;

}



sub handler_gender
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  # trunctae middle name to length 1
  my $nm = uc(substr($acct_row_hash_ref->{"gender"}, 0, 1));

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $nm;

  return 1;

}



sub handler_pat_proc_type
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  # AFTER TREATMENT ALL INPATIENT SHOULD BE IDENTIFIED WITH VALUE: I
  # ALL NULL VALUES SHOULD BE SET TO: O 
  if 
         ($acct_row_hash_ref->{"cust_id"} == 1000)                         {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 'O';}
  
  elsif ($acct_row_hash_ref->{"cust_id"} == 25 
         &&
         $acct_row_hash_ref->{"pat_type"} eq "WINP")         {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 'I';} 
  elsif
     ($acct_row_hash_ref->{"cust_id"} == 25)                 {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 'O';}
  elsif ($acct_row_hash_ref->{"pat_proc_type"} eq "")        {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 'O';}
  else                                                       {$batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{"pat_proc_type"};}
 
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc(substr($batch_acct_row_hash_ref->{$batch_acct_col_name}, 0, 1));

  return 1;

}



sub handler_obs_ind
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  # define as Y when service type is OBSERVATION 
  # below treat Piedmont (cust_id = 9) , NGHS (cust_id = 1) and Saint Joseph (cust_id = 21) 
  if  ($acct_row_hash_ref->{"cust_id"} == 9 
         && 
         $acct_row_hash_ref->{"pat_type"} eq "OBS")                {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 1
         &&
         substr($acct_row_hash_ref->{"pat_type"}, 0, 4) eq 'OBV-') {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 21
         &&
         $acct_row_hash_ref->{"pat_proc_type"} eq 'V')             {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 25
         &&
         $acct_row_hash_ref->{"pat_type"} eq "WOBS")               {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 34 
         &&
         $acct_row_hash_ref->{"pat_type"} eq "OBV")                {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 32
         &&
          ($acct_row_hash_ref->{"pat_type"} eq "OBV" || $acct_row_hash_ref->{"pat_type"} eq "OBS")
         )                                                         {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 36
         &&
         ($acct_row_hash_ref->{"pat_type"} eq "OBSERVATION")
        )                                                          {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 38
         &&
         ($acct_row_hash_ref->{"pat_type"} eq "SMOBS")
        )                                                          {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 40 
         &&
         ($acct_row_hash_ref->{"pat_type"} eq "OBSEROUT")
        )                                                          {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  elsif ($acct_row_hash_ref->{"cust_id"} == 48
         &&
         ($acct_row_hash_ref->{"pat_type"} eq "B" || $acct_row_hash_ref->{"pat_type"} eq "6")
        )                                                          {$batch_acct_row_hash_ref->{$batch_acct_col_name}='Y';}
  else                                                             {$batch_acct_row_hash_ref->{$batch_acct_col_name}='N';}
  return 1;
}




sub handler_pat_type
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $tp = "";

  # we receive NGHS pat type as: service_type-pat_type so we extract pat_type 
  if    ($acct_row_hash_ref->{"cust_id"} == 1) { 
         
          $tp=$acct_row_hash_ref->{"pat_type"}; 
          $tp=~ s/^(.)*-//g;                     
          $batch_acct_row_hash_ref->{$batch_acct_col_name}=$tp;
        } 
  else  {$batch_acct_row_hash_ref->{$batch_acct_col_name}=$acct_row_hash_ref->{"pat_type"};}

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($batch_acct_row_hash_ref->{$batch_acct_col_name},0,25);
  return 1;
}




sub handler_state
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);


  if ($acct_row_hash_ref->{$batch_acct_col_name} eq '00') {
    $batch_acct_row_hash_ref->{$batch_acct_col_name} = "";
  }
  else { $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($acct_row_hash_ref->{$batch_acct_col_name}); }

    $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/^(\w{2}).*$/$1/ ;

  return 1;
}




sub handler_address
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $tp = "";

  $tp = uc($acct_row_hash_ref->{$batch_acct_col_name});
  $tp = substr($tp, 0, 50);
  $tp =~ s/\./ /g;

  if ($tp =~ m/UPDATE ADDRESS/i || $tp =~ m/BAD ADDRESS/i || $tp eq 'UNK' || $tp eq 'UNKNOWN') {
    $batch_acct_row_hash_ref->{$batch_acct_col_name} = "";
  } 
  else {
      $batch_acct_row_hash_ref->{$batch_acct_col_name} = $tp;
  }

  return 1;
}




sub handler_city
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $tp = "";

  $tp = uc($acct_row_hash_ref->{$batch_acct_col_name});
  $tp = substr($tp, 0, 30);
  $tp =~ s/\./ /g;

  if ($acct_row_hash_ref->{"cust_id"} == 23 && $tp eq 'PHILA') {
      $batch_acct_row_hash_ref->{$batch_acct_col_name} = "PHILADELPHIA";
  }
  else {
      $batch_acct_row_hash_ref->{$batch_acct_col_name} = $tp;
  }

  return 1;
}




sub handler_upper
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);


  $batch_acct_row_hash_ref->{$batch_acct_col_name} = uc($acct_row_hash_ref->{$batch_acct_col_name});
  if ( $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ /\w+\s+\w+/ ) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/(\w+)\s+(\w+)/$1 $2/;  #limit the space in middle
  }
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/\./ /g;


  return 1;
}





sub loc_format_date_str {
	my $datstr = shift(@_);

	my $y1 = "";
	my $m1 = "";
	my $d1 = "";
	my $retstr = "";
        my $test = "";

	chomp $datstr;
	$datstr =~ s/\.//g;       #remove  '.' 
	$datstr =~ s/\s//g;       #remove any space 
        $test = $datstr;
        $test =~ s/(\/|0)//g; 

	if ( $datstr eq "" || $test eq "") { 
		return "";
	}

	my $len = length($datstr); 
	if( $len == 6 ) { 
		$m1 = substr($datstr,0,2);
		$d1 = substr($datstr,2,2);
		$y1 = substr($datstr,4,2);  
                if ( $m1 eq '00' || $d1 eq '00') { $retstr = "";}
                else {$retstr = "$m1/$d1/$y1";}       # insert the slashes
	}
	elsif ( $len == 8  && $datstr !~ /\// && $datstr !~ /-/) { 
		$m1 = substr($datstr,0,4);
		$d1 = substr($datstr,4,4);
		if ( $m1 < $d1 ) {  # mmddyyyy
			$m1 = substr($datstr,0,2);
			$d1 = substr($datstr,2,2);
			$y1 = substr($datstr,4,4);  
		} 
		else {				# yyyyymmdd
			$y1 = substr($datstr,0,4);  
			$m1 = substr($datstr,4,2);
			$d1 = substr($datstr,6,2);
		}
                if ( $m1 eq '00' || $d1 eq '00') { $retstr = "";}
                else {$retstr = "$m1/$d1/$y1";}      
	}
	elsif ( $len == 10 ) { # mm-dd-yyyy  or  yyyy/mm/dd
		($m1, $d1, $y1) = split (/[-\/]/, $datstr);	
		if( length($m1) > 2 ) {
			($y1, $m1, $d1) = split (/[-\/]/, $datstr);	
		}
                if ( $m1 eq '00' || $d1 eq '00') { $retstr = "";}
                else {$retstr = "$m1/$d1/$y1";}       # insert the slashes
	}
	else {
		$retstr = "";  
	}
	
	return $retstr;
}


sub handler_date
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  my $activity1="";		
  my $y1 = "";
  my $m1 = "";
  my $d1 = "";

  $activity1 = $acct_row_hash_ref->{$batch_acct_col_name};
	# $activity1 <-- input date string
  $activity1 = loc_format_date_str( $activity1);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $activity1;

  return 1;
}



sub handler_service_date_end
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  my $activity1="";
  my $y1 = "";
  my $m1 = "";
  my $d1 = "";

  $activity1 = $acct_row_hash_ref->{'service_date_end'};
  chomp $activity1;
  $activity1 =~ s/\.//g ;

	if ($activity1 =~ /^\s*$/ ) { 
		if ( defined $acct_row_hash_ref->{'service_date_begin'} ) {
			$activity1 = $acct_row_hash_ref->{'service_date_begin'};
		}
	}

	if ( $activity1 ne "") {
		$activity1 = loc_format_date_str( $activity1);
	}

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $activity1;

  return 1;
}



sub handler_zip5
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{$batch_acct_col_name};

  # pad with zeroes
  if    (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) > 5) {
		my $zstr = $batch_acct_row_hash_ref->{$batch_acct_col_name};
		if ( $zstr =~ /-/ ) {	# [zip5]-[route code] format
			$zstr =~ s/\s//g;
			my($zp5, $zp4) = split(/-/, $zstr);
      		$batch_acct_row_hash_ref->{$batch_acct_col_name} = $zp5;
			my $tmp_zip4_col_name = $batch_acct_col_name;
			$tmp_zip4_col_name =~ s/5/4/;
      		$acct_row_hash_ref->{$tmp_zip4_col_name} = $zp4;
		}
		else {
      		$batch_acct_row_hash_ref->{$batch_acct_col_name} = "";
		}
  }

  if (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 1) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0000'.$batch_acct_row_hash_ref->{$batch_acct_col_name};
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 2) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '000'.$batch_acct_row_hash_ref->{$batch_acct_col_name};
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 3) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '00'.$batch_acct_row_hash_ref->{$batch_acct_col_name};
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 4) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0'.$batch_acct_row_hash_ref->{$batch_acct_col_name};
  }

  if ($batch_acct_row_hash_ref->{$batch_acct_col_name} eq "00000" || $batch_acct_row_hash_ref->{$batch_acct_col_name} eq "0000")
  { $batch_acct_row_hash_ref->{$batch_acct_col_name} = "";}

  return 1;
}


sub handler_zip4
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{$batch_acct_col_name};

  # pad with zeroes
  if    (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) > 4) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = "";
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 1) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 2) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '00'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 3) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }

  if ($batch_acct_row_hash_ref->{$batch_acct_col_name} eq "0000" || $batch_acct_row_hash_ref->{$batch_acct_col_name} eq "000")
      { $batch_acct_row_hash_ref->{$batch_acct_col_name} = "";}

  return 1;
}


sub handler_ssn
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);
  my $ssn="";

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{$batch_acct_col_name};

  # remove the hypen between the ssn numbers and add leading zeroes if needed
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/-//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/ //g;

  # pad with zeroes
  if (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 1) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '00000000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 2) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0000000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 3) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '000000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 4) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '00000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 5) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 6) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '000'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 7) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '00'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }
  elsif (length($batch_acct_row_hash_ref->{$batch_acct_col_name}) == 8) {
          $batch_acct_row_hash_ref->{$batch_acct_col_name} = '0'.$batch_acct_row_hash_ref->{$batch_acct_col_name}
  }

  # treat defaults
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/(000000000|^00000000$|111111111|222222222|333333333|444444444|555555555|666666666|777777777|888888888|999999999|099999999)//g;

  if ( ($batch_acct_row_hash_ref->{$batch_acct_col_name} ne "")
      && 
      (
         substr($batch_acct_row_hash_ref->{$batch_acct_col_name},0,3) eq '000'
         ||
         substr($batch_acct_row_hash_ref->{$batch_acct_col_name},0,3) > '800' 
         ||
         substr($batch_acct_row_hash_ref->{$batch_acct_col_name},0,3) eq '666'
         ||
         substr($batch_acct_row_hash_ref->{$batch_acct_col_name},3,2) eq '00'
         ||
         substr($batch_acct_row_hash_ref->{$batch_acct_col_name},5,3) eq '0000'
      )
     )
      {$batch_acct_row_hash_ref->{$batch_acct_col_name} = "";}

  return 1;

}


sub handler_phone
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = $acct_row_hash_ref->{$batch_acct_col_name};

  # strip characters from phone fields 
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~ s/[()\- ]//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} = substr($batch_acct_row_hash_ref->{$batch_acct_col_name},0,14);

  return 1;

}



sub handler_employ_name
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  if (  $acct_row_hash_ref->{$batch_acct_col_name} ne "" ) 
    {$batch_acct_row_hash_ref->{$batch_acct_col_name} = uc(substr($acct_row_hash_ref->{$batch_acct_col_name},0,35));}

  return 1;

}



sub handler_currency_negate
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} = 
                              $acct_row_hash_ref->{$batch_acct_col_name};

  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/\$\((.*)\)/-$1/;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/\$//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/,//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/ //g;

  if ( $batch_acct_row_hash_ref->{$batch_acct_col_name} eq "" ) {$batch_acct_row_hash_ref->{$batch_acct_col_name} = 0;}

  return 1;
}



sub handler_charges
{
  my $batch_acct_col_name = shift(@_);
  my $acct_row_hash_ref = shift(@_);
  my $batch_acct_row_hash_ref = shift(@_);

  $batch_acct_row_hash_ref->{$batch_acct_col_name} =
                              $acct_row_hash_ref->{$batch_acct_col_name};

  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/\$\((.*)\)/-$1/;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/\$//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/,//g;
  $batch_acct_row_hash_ref->{$batch_acct_col_name} =~  s/ //g;

  return 1;
}



# all handlers should be defined above this
sub register_batch_acct_handlers()
{
  # STANDARD HANDLERS 
  $g_dbconv->register_handler("our_run_date", \&handler_our_run_date);        # Create our_run_date Field
  $g_dbconv->register_handler("seqnum", \&handler_seqnum);                    # Create seqnum = File Record Number

  # POPULATE NULL VALUES WITH DEFAULTS 
  $g_dbconv->register_handler("hid", \&handler_hid);
  $g_dbconv->register_handler("treatment_code", \&handler_treatment_code);
  $g_dbconv->register_handler("output_id", \&handler_output_id);

  # STANDARD HANDLERS - INSERT null VALUES - DO NOT MODIFY THESE LINES
  $g_dbconv->register_handler("name_type", \&handler_default);
  $g_dbconv->register_handler("medicaid_max_date", \&handler_default);
  $g_dbconv->register_handler("medicare_max_date", \&handler_default);
  $g_dbconv->register_handler("third_party_sent_date", \&handler_default);
  $g_dbconv->register_handler("hippa_flag", \&handler_default);
  $g_dbconv->register_handler("emdeon_sent_date", \&handler_default);
  $g_dbconv->register_handler("emdeon_commercial_flag", \&handler_default);
  $g_dbconv->register_handler("emdeon_tpl_flag", \&handler_default);
  $g_dbconv->register_handler("emdeon_medicaid_flag", \&handler_default);
  $g_dbconv->register_handler("emdeon_medicare_flag", \&handler_default);
  $g_dbconv->register_handler("emdeon_medicare_fast", \&handler_default);
  $g_dbconv->register_handler("threshold_commercial", \&handler_default);
  $g_dbconv->register_handler("threshold_tpl", \&handler_default);
  $g_dbconv->register_handler("threshold_medicaid", \&handler_default);
  $g_dbconv->register_handler("threshold_medicare", \&handler_default);
  $g_dbconv->register_handler("threshold_bal_commercial", \&handler_default);
  $g_dbconv->register_handler("threshold_bal_tpl", \&handler_default);
  $g_dbconv->register_handler("threshold_bal_medicaid", \&handler_default);
  $g_dbconv->register_handler("threshold_bal_medicare", \&handler_default);
  $g_dbconv->register_handler("minimum_charges", \&handler_default);
  $g_dbconv->register_handler("threshold_commercial_day", \&handler_default);
  $g_dbconv->register_handler("threshold_tpl_day", \&handler_default);
  $g_dbconv->register_handler("threshold_medicaid_day", \&handler_default);
  $g_dbconv->register_handler("threshold_medicare_day", \&handler_default);
  $g_dbconv->register_handler("Ithreshold_commercial_day", \&handler_default);
  $g_dbconv->register_handler("Ithreshold_tpl_day", \&handler_default);
  $g_dbconv->register_handler("Ithreshold_medicaid_day", \&handler_default);
  $g_dbconv->register_handler("Ithreshold_medicare_day", \&handler_default);
  $g_dbconv->register_handler("prior_commercial_run", \&handler_default);
  $g_dbconv->register_handler("prior_tpl_run", \&handler_default);
  $g_dbconv->register_handler("prior_medicaid_run", \&handler_default);
  $g_dbconv->register_handler("prior_medicare_run", \&handler_default);
  $g_dbconv->register_handler("current_commercial_run", \&handler_default);
  $g_dbconv->register_handler("current_tpl_run", \&handler_default);
  $g_dbconv->register_handler("current_medicaid_run", \&handler_default);
  $g_dbconv->register_handler("current_medicare_run", \&handler_default);
  $g_dbconv->register_handler("calc_group", \&handler_default);
  $g_dbconv->register_handler("processing_type", \&handler_default);
  $g_dbconv->register_handler("out_of_state", \&handler_default);
  $g_dbconv->register_handler("individual_matchkey", \&handler_default);
  $g_dbconv->register_handler("processing_ssn_flag", \&handler_default);
  $g_dbconv->register_handler("commercial_score", \&handler_default);
  $g_dbconv->register_handler("scoring_code_commercial", \&handler_default);
  $g_dbconv->register_handler("tpl_score", \&handler_default);
  $g_dbconv->register_handler("scoring_code_tpl", \&handler_default);
  $g_dbconv->register_handler("medicaid_score", \&handler_default);
  $g_dbconv->register_handler("scoring_code_medicaid", \&handler_default);
  $g_dbconv->register_handler("medicare_score", \&handler_default);
  $g_dbconv->register_handler("scoring_code_medicare", \&handler_default);

  # SET 00 AS NULL AND SET VALUE TO UPPERCASE
  $g_dbconv->register_handler("provider_state", \&handler_state);
  $g_dbconv->register_handler("state", \&handler_state);  
  $g_dbconv->register_handler("g_state", \&handler_state);  

  # THE FOLLOWING FIELDS MUST BE SET TO UPPERCASE;
  $g_dbconv->register_handler("mname", \&handler_mname);
  $g_dbconv->register_handler("lname", \&handler_lastname);
  $g_dbconv->register_handler("fname", \&handler_upper);
  $g_dbconv->register_handler("g_mname", \&handler_mname);
  $g_dbconv->register_handler("g_lname", \&handler_lastname);
  $g_dbconv->register_handler("g_fname", \&handler_upper);


  # TO MODIFY: ADD HANDLERS FOR STANDARD FIELDS (TABLE 2 of DOCUMENTATION) NOT SENT BY CLIENT
 

###############################################################
  # BELOW ARE FIELDS SENT BY CLIENT REQUIRING SPECIAL PROCESSING

  # TRUNCATE FIELDS 
  # POPULATE FIELDS client_npi2 - entity_phone2 WITH VALUES FROM client_npi1 - entity_phone1 
  $g_dbconv->register_handler("client_npi1", \&handler_client_npi);
  $g_dbconv->register_handler("client_npi2", \&handler_client_npi);
  $g_dbconv->register_handler("npi2", \&handler_npi);
  $g_dbconv->register_handler("tax_id2", \&handler_tax_id);
  $g_dbconv->register_handler("legacy2", \&handler_legacy);
  $g_dbconv->register_handler("entity_name1", \&handler_entity_name);
  $g_dbconv->register_handler("entity_name2", \&handler_entity_name);
  $g_dbconv->register_handler("entity_pay_to_street1", \&handler_entity_pay_to_street);
  $g_dbconv->register_handler("entity_pay_to_street2", \&handler_entity_pay_to_street);
  $g_dbconv->register_handler("entity_pay_to_city1", \&handler_entity_pay_to_city);
  $g_dbconv->register_handler("entity_pay_to_city2", \&handler_entity_pay_to_city);
  $g_dbconv->register_handler("entity_pay_to_state1", \&handler_entity_pay_to_state);
  $g_dbconv->register_handler("entity_pay_to_state2", \&handler_entity_pay_to_state);
  $g_dbconv->register_handler("entity_pay_to_zip1", \&handler_entity_pay_to_zip);
  $g_dbconv->register_handler("entity_pay_to_zip2", \&handler_entity_pay_to_zip);
  $g_dbconv->register_handler("entity_information1", \&handler_entity_information);
  $g_dbconv->register_handler("entity_information2", \&handler_entity_information);
  $g_dbconv->register_handler("entity_phone1", \&handler_entity_phone);
  $g_dbconv->register_handler("entity_phone2", \&handler_entity_phone);


  # ADDRESS CLEANING  
  $g_dbconv->register_handler("addr1", \&handler_address);
  $g_dbconv->register_handler("addr2", \&handler_address);
  $g_dbconv->register_handler("g_addr1", \&handler_address);
  $g_dbconv->register_handler("g_addr2", \&handler_address);

  # CREATE DEFAULT VALUE 1 FOR acctnum_seq
  $g_dbconv->register_handler("acctnum_seq", \&handler_acctnum_seq);

  # CITY CLEANING
  $g_dbconv->register_handler("city", \&handler_city);
  $g_dbconv->register_handler("g_city", \&handler_city);

  # NOTE ALWAYS USE A HANDLER TO PAD zips WITH LEADING 0
  $g_dbconv->register_handler("zip5", \&handler_zip5);
  $g_dbconv->register_handler("zip4", \&handler_zip4);
  $g_dbconv->register_handler("g_zip5", \&handler_zip5);
  $g_dbconv->register_handler("g_zip4", \&handler_zip4);

  # BELOW IS STANDARD TREATMENT FOR ssn AND g_ssn
  # NOTE ALWAYS USE A HANDLER TO PAD g_ssn WITH LEADING 0 AND TREAT DEFAULTS
  $g_dbconv->register_handler("ssn", \&handler_ssn);
  $g_dbconv->register_handler("g_ssn", \&handler_ssn);

  # TREAT pat_proc_type.  TRUNCATE TO ONE BYTE AND INPATIENT MUST BE REPRESENTED BY VALUE I
  $g_dbconv->register_handler("pat_proc_type", \&handler_pat_proc_type);

  # TREAT relationship_flag.  TRUNCATE TO THREE BYTES
  $g_dbconv->register_handler("relationship_flag", \&handler_relationship);

  # TREAT pat_type  
  $g_dbconv->register_handler("pat_type", \&handler_pat_type);

  # CREATE THE FIELD obs_ind 
  $g_dbconv->register_handler("obs_ind", \&handler_obs_ind);

  # truncating some fields 
  $g_dbconv->register_handler("gender", \&handler_gender);
  $g_dbconv->register_handler("g_gender", \&handler_gender);
  
  # TREAT g_employ_name 
  $g_dbconv->register_handler("g_employ_name", \&handler_employ_name);

  # conver currency format i.e $("xxx.xx) to negative
  # strip $ signs
  $g_dbconv->register_handler("balance", \&handler_charges);
  $g_dbconv->register_handler("charges", \&handler_charges);

  # date treatment;
  $g_dbconv->register_handler("service_date_begin", \&handler_date);
  $g_dbconv->register_handler("service_date_end", \&handler_date);
  $g_dbconv->register_handler("dob", \&handler_date);
  $g_dbconv->register_handler("g_dob", \&handler_date);

}


sub DESTROY
{
  print STDERR "EXIT CLEANUP\n";
  if(defined $g_dbconn) {
    $g_dbconn->disconnect;
  }
}


# main process args
if ( @ARGV && (( $ARGV[0] =~ /-h/ ) || (($#ARGV % 2) == 0)) ) {
  usage();
  exit(1);
}

if( ! init_args(@ARGV) ) {
  usage();
  exit 2;
}

$g_dbconn = MLDBUtils::DBConnect->new($g_dbname, $g_dbuser, $g_dbpasswd, \$g_err);
if ( ! defined $g_dbconn ) {
  print STDERR "Unable to connect to DB : $g_err\n";
  exit 3;
}

# get hospital id
$g_dbcust = MLDBUtils::DBCustID->new($g_dbconn, \$g_err);
if ( ! defined $g_dbconn ) {
  print STDERR "Unable to get cust db info : $g_err\n";
  exit 4;
}

if(!$g_dbcust->custid_by_name( $g_dbschema, \$g_cust_id )) {
  print STDERR "Unable to get cust id from db : ", $g_dbcust->errstr, "\n";
  exit 4;
}

if(! defined $g_cust_id || ( $g_cust_id eq "" ) || ($g_cust_id <= 0)) {
  print STDERR "Unable to get hospital system id from db for cust : ", $g_dbschema, "\n";
  exit 4;
}


$g_dbconv = MLDBUtils::DBConvert->new
                 (
                   \$g_err,
                   {
                     dbh           => $g_dbconn,
                     in_tbl_name   => $g_in_tbl_name,
                     out_tbl_name  => $g_out_tbl_name,
                     in_hdr_file   => $g_acct_hdr_file,
                     out_hdr_file  => $g_batch_acct_hdr_file,
                     in_data_file  => $g_acct_data_file,
                     out_data_file => $g_batch_acct_data_file,
                   }
                 );

if( !defined $g_dbconv) {
  print STDERR "Unable to convert : ", $g_err, "\n";
  exit 4;
}

register_batch_acct_handlers();

$g_exitcode = !$g_dbconv->convert();



$g_dbconn->commit;
#$g_dbconn->rollback;
if($g_exitcode) {
  print STDERR "${PGNAME} : conversion failed : $g_dbconv->{errstr}\n";
  $g_batch_acct_file="$ENV{'MLPROD_HOME'}/batch/$g_dbschema/" 
                        . $g_batch_recv_date
                        . "_${g_cust_id}_acct.gpg";
  unlink("$g_batch_acct_file");
}

$g_dbconn->disconnect;
$g_dbconn = undef;

exit($g_exitcode);
